package com.liyunc.demo.model.repository;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.liyunc.demo.model.entity.RoleInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * @author
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-02-17
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-17                       Created
 */
@Repository
public interface RoleInfoRepository extends JpaRepository<RoleInfo, Long>,
    JpaSpecificationExecutor<RoleInfo> {

    @Query(value = "SELECT project_id FROM role_info WHERE user_id = ?", nativeQuery = true)
    List<Long> findProjectIdByUserId(Long userId);

    @Query(value = "SELECT roleInfo.*"
        + " FROM user_info userInfo"
        + " LEFT JOIN user_role userRole ON userInfo.id = userRole.user_id"
        + " LEFT JOIN role_info roleInfo ON roleInfo.id = userRole.role_id"
        + " WHERE userInfo.id = ?1", nativeQuery = true)
    Set<RoleInfo> findAllByUserId(final Long userId);
}
